import http from "@src/config/http.ts"

const baseURL = "http://8.138.152.233:3000"

//https://binaryify.github.io/NeteaseCloudMusicApi/#/
interface ApiType {
    [key: string]: string;
}

// api接口 - 此处用了统一保存接口url路径
const api: ApiType = {
    playlistTrackAll: '/playlist/track/all', //获取歌单所有歌曲
    songUrl: "/song/url",//获取音乐播放链接
    checkStatus: "/login/qr/check", //二维码检测扫码状态接口
    getLoginStatus: `/login/status?timestamp=${Date.now()}`,//登录状态
    qrKey: "/login/qr/key",// 二维码 key 生成接口
    qrCreate: "/login/qr/create",//二维码生成接口
    loginRefresh: "/login/refresh",//刷新登录
    searchSuggest: "/search/suggest", //搜索提示
    search: "/cloudsearch", //获取搜索结果
    topPlaylist: "/top/playlist",//获取歌单列表
    highQuality: "/top/playlist/highquality", //获取精品歌单
    playlistHot: "/playlist/hot",//热门歌单分类
    playlistCatList: "/playlist/catlist", //获取分类歌单关键词
    lyric: "/lyric", //获取歌词
};


for (const key in api) {
    api[key] = `${baseURL}${api[key]}`;
}


export interface playlistTrackAllParametersType {
    id: number;
    limit?: number;
    offset?: number
}

interface playlistTrackDataType {
    code: number;
    songs: songsType[]
}

export interface songsType {
    id: number;
    name: string;
    al: al;
    ar: ar[];
    dt: number;
}

interface al {
    id: number;
    picUrl: string
}

interface ar {
    id: number;
    name: string;
}

export interface songUrlDataType {
    data: Array<songType>
}

interface songType {
    id: number;
    url: string;
    time: number;
    type: string;
    size: number;
}

interface checkStatusDataType {
    data: string;
    code: number;
    cookie: string;
}

interface qrKeyDataType {
    data: {
        unikey: string
    }
}

interface qrCreateDataType {
    data: {
        qrimg: string
    }
}

//歌单类型
export interface playlistType {
    id: number;
    name: string;
    coverImgUrl: string;
    playCount: number;
    description: string
    tags: string[],
    creator: { avatarUrl: string, nickname: string };
}

export interface playlistHotDataType {
    tags: Array<playlistHotDataTagsType>
}

interface playlistHotDataTagsType {
    id: number;
    name: string;
    type: number;
}

//分类
export interface subType {
    name: string,
    category: number
}

//分类歌单提示类型
export interface playlistCatListDataType {
    sub: Array<subType>
    categories: Record<string, string>
}

//搜索提示类型
export interface searchSuggestDataType {
    result: { allMatch?: Array<{ keyword: string; type: number; }> }
}

//获取歌单所有歌曲
export function playlistTrackAll(data: playlistTrackAllParametersType) {
    return http.get<playlistTrackDataType>(api.playlistTrackAll, data);
}

//获取歌曲播放链接
export function songUrl(id: number) {
    return http.get<songUrlDataType>(api.songUrl, {id: id, cookie: localStorage.getItem("cookie")});
}


//二维码检测扫码状态接口
export function checkStatus(key: string) {
    return http.get<checkStatusDataType>(api.checkStatus, {
        key: key,
        timestamp: Date.now(),
        noCookie: true
    });
}

//登录状态
export function getLoginStatus(cookie: string) {
    return http.post<checkStatusDataType>(api.getLoginStatus, {cookie: cookie});
}


// 二维码 key 生成接口
export function qrKey() {
    return http.get<qrKeyDataType>(api.qrKey, {timestamp: Date.now()})
}

//二维码生成接口
export function qrCreate(unikey: string) {
    return http.get<qrCreateDataType>(api.qrCreate, {
        key: unikey,
        qrimg: true,
        timestamp: Date.now()
    });
}

//刷新登录 cookie
export function loginRefresh() {
    return http.get<qrCreateDataType>(api.loginRefresh)
}

export interface highQualityDataType {
    playlists: Array<playlistType>
}


//获取精品歌单
export function highQuality(limit: number = 9) {
    return http.get<highQualityDataType>(api.highQuality, {limit: limit})
}

//获取热门歌单分类
export function playlistHot() {
    return http.get<playlistHotDataType>(api.playlistHot)
}


//搜索提示
export function getSearchSuggest(keywords: string, type = "mobile") {
    return http.get<searchSuggestDataType>(api.searchSuggest, {keywords: keywords, type: type});
}

//搜索结果类型
export interface searchDataType {
    result: {
        songs: Array<songsType>,
    }
}

//搜索歌曲
export function search(keywords: string, limit = 30, offset = 0, type = 1) {
    return http.get<searchDataType>(api.search, {keywords: keywords, limit: limit, offset: offset, type: type});
}


//获取分类提示
export function playlistCatList() {
    return http.get<playlistCatListDataType>(api.playlistCatList);
}


//获取歌单列表
export function topPlaylist(cat: string, limit: number = 30, offset: number = 1) {
    return http.get<highQualityDataType>(api.topPlaylist, {cat: cat, limit: limit, offset: (offset * limit)});
}

interface lyricDataType {
    lrc: {
        lyric: string
    }
}

//获取歌词
export function lyric(id: number) {
    return http.get<lyricDataType>(api.lyric, {id: id});
}